#include "playercontroller.h"
#include "qaxobject.h"

#include <QFileDialog>
#include <QStandardPaths>
#include <QDebug>

PlayerController::PlayerController(QObject *parent, QAxWidget* axWidget): QObject{parent} {
    this->playState = IDLE;
    this->axWidget = axWidget;
    axWidget->setControl(this->CLSID);
    controls = axWidget->querySubObject("controls");

    connect(axWidget, SIGNAL(PlayStateChange(int)), this, SLOT(onPlayStateChange(int)));
    connect(axWidget, SIGNAL(PositionChange(double,double)), this, SLOT(onPositionChange(double,double)));
}

PlayerController::~PlayerController(){
    axWidget->close();
}

void PlayerController::onPlayStateChange(int newState){
    playState = (PlayerState)newState;
    CommonMessage msg = CommonMessage(PlayerStateChange, QString::number(playState));
    emit sendMsgToMain(LocalPlayer, msg);
}

void PlayerController::onPositionChange(double oldP, double newP){
    // 防止视频抖动
    if(qAbs(oldP - newP) < 1e-4){
        return;
    }
    CommonMessage msg = CommonMessage(PlayerProgessChange, QString::number(newP));
    emit sendMsgToMain(LocalPlayer, msg);
}

PlayerState PlayerController::getPlayState(){
    return this->playState;
}

bool PlayerController::setPlayState(PlayerState state){
    switch(state){
        case STOP:
            stop();
            break;
        case PAUSE:
            pause();
            break;
        case PLAYING:
            start();
            break;
        default:
            pause();
            // qDebug() << "其他状态:" << state;
            return false;
    }
    return true;
}

void PlayerController::setCurrentPosition(QString pos){
    controls->setProperty("currentPosition", pos.toDouble());
}

QString PlayerController::getProcessBarInfo(){
    // QString pos = controls->dynamicCall("currentPositionString").toString();
    QString pos = controls->dynamicCall("currentPosition").toString();
    return pos;
}

void PlayerController::stop(){
    controls->dynamicCall("stop()");
}

void PlayerController::pause(){
    controls->dynamicCall("pause()");
}

void PlayerController::start(){
    controls->dynamicCall("play()");
}

void PlayerController::openFile(QString path){
    axWidget->dynamicCall("URL", path);//设置组件的属性，也可利用setProperty()函数
    axWidget->dynamicCall("setStretchToFit", true);//拉伸适应屏幕
}

MediaInfo PlayerController::getCurrentMediaInfo(){
    MediaInfo info;
    currentMedia = axWidget->querySubObject("currentMedia");
    info.title = currentMedia->dynamicCall("getItemInfo(QString)","title").toString()
                + "." + currentMedia->dynamicCall("getItemInfo(QString)","fileType").toString();
    info.duration = currentMedia->dynamicCall("getItemInfo(QString)","duration").toString();
    return info;
}

void PlayerController::setFullScreen(){
    axWidget->dynamicCall("setFullScreen", true);
}

//静态方法,用于转换
QString PlayerController::stateToString(PlayerState state){
    switch(state){
        case STOP:
            return "stop";
        case PAUSE:
            return "pause";
        case PLAYING:
            return "playing";
        case END:
            return "end";
        case READY:
            return "ready";
        case IDLE:
            return "--";
    }
    return "--";
}

QString PlayerController::stateToString(QString state){
    return stateToString((PlayerState)state.toInt());
}

QString PlayerController::timeToString(double time){
    int cur = (int)time;
    int hour = cur / 3600;
    int min = (cur - hour * 3600) / 60;
    int sec = cur % 60;
    return QString("%1:%2:%3").arg(hour, 2, 10, QLatin1Char('0')).arg(min, 2, 10, QLatin1Char('0')).arg(sec, 2, 10, QLatin1Char('0'));
}

QString PlayerController::timeToString(QString time){
    return timeToString(time.toDouble());
}
